<?php

define('APPTYPEID', 101);
define('CURSCRIPT', 'tuan_pay');

require '../../../../source/class/class_core.php';
include_once(DISCUZ_ROOT.'/source/plugin/dzapp_zhibo/class/payPubHelper.class.php');

C::app()->init();

$notify = new Notify_pub();
$xml = file_get_contents("php://input");

$notify->saveData($xml);

if ($notify->checkSign() == FALSE) {
    $notify->setReturnParameter("return_code", "FAIL");
    $notify->setReturnParameter("return_msg", "SIGN_FAIL");
  
} else {
    $notify->setReturnParameter("return_code", "SUCCESS");
}

$returnXml = $notify->returnXml();
echo $returnXml;


if ($notify->data["return_code"] == "FAIL") {

} elseif ($notify->data["result_code"] == "FAIL") {

} else {
		
	$order_id = str_replace('zhibo', '', $notify->data['out_trade_no']);
	
	$order = C::t('#dzapp_zhibo#dzapp_zhibo_order')->fetch($order_id);
	
	if(!$order['pay_time']){
		$setarr = array(
			'pay_time' => $_G['timestamp'],
		);
		
		C::t('#dzapp_zhibo#dzapp_zhibo_order')->update($order_id, $setarr);
		
		$gift = C::t('#dzapp_zhibo#dzapp_zhibo_gift')->fetch($order['gift_id']);
		
		$setarr = array(
			'table' => 'dzapp_zhibo_activity',
			'id' => $order['activity_id'],
			'uid' => $order['uid'],
			'username' => $order['username'],
			'message' => '<div class="gift_send">'.lang('plugin/dzapp_zhibo','send_a').$gift['name'].'</div>',
			'dateline' => $_G['timestamp'],
			'ip' => $_G['ip'],
		);
		
		C::t('#dzapp_zhibo#dzapp_zhibo_comment')->insert($setarr);
	}
}

?>
